package com.level2app.utils;

import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import android.view.LayoutInflater;
import android.view.View;

import com.level2app.R;
import com.level2app.base.MyApplication;

/**
 * Toast工具类
 */
public final class ToastHelper {
    private static Toast toast;

    public static void showToast(String text) {
        LayoutInflater inflater = LayoutInflater.from(MyApplication.getContext());
        View view = inflater.inflate(R.layout.layout_toast_view, null);
        ImageView imageView = view.findViewById(R.id.iv_toast);
        imageView.setVisibility(View.GONE);
        TextView t = view.findViewById(R.id.tv_toast_title);
        t.setText(text);
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(MyApplication.getContext());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP + Gravity.FILL_HORIZONTAL, 0, ScreenUtil.dip2px(96));
        toast.setView(view);
        toast.show();
    }

    public static void showToastAndImage(String text, int imageResource) {
        LayoutInflater inflater = LayoutInflater.from(MyApplication.getContext());
        View view = inflater.inflate(R.layout.layout_toast_view, null);
        ImageView imageView = view.findViewById(R.id.iv_toast);
        imageView.setImageResource(imageResource);
        TextView t = view.findViewById(R.id.tv_toast_title);
        t.setText(text);
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(MyApplication.getContext());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP + Gravity.FILL_HORIZONTAL, 0, ScreenUtil.dip2px(96));
        toast.setView(view);
        toast.show();
    }
}